package com.myapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.myapplication.model.CartInfo;
import com.myapplication.zhou.alldemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhoushikun on 2016/7/13.
 */
public class CartInfoRecycleAdapter extends RecyclerView.Adapter<CartInfoRecycleAdapter.CartViewHolder> {

    private Context context;
    private List<CartInfo> cartInfos = new ArrayList<>();
    private LayoutInflater inflater;
    private ItemClickListener itemClickListener;
    private JiaJianClickListener jiaJianClickListener;
    public BitmapRequestBuilder<GlideUrl,Bitmap> requestBuilder;


    public CartInfoRecycleAdapter(Context context,List<CartInfo> cartInfos){
        this.context = context;
        this.cartInfos = cartInfos;
        this.inflater = LayoutInflater.from(context);
        requestBuilder = Glide.with(context)
                .from(GlideUrl.class)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .error(R.mipmap.wutu)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
    }

    @Override
    public CartInfoRecycleAdapter.CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.cart_info_recycler_view,parent,false);
        CartViewHolder cartViewHolder = new CartViewHolder(view,itemClickListener,jiaJianClickListener);

        return cartViewHolder;
    }

    @Override
    public void onBindViewHolder(CartInfoRecycleAdapter.CartViewHolder holder, int position) {
        holder.bindView(cartInfos.get(position));
        requestBuilder.load(new GlideUrl(cartInfos.get(position).getGoods_image_url())).into(holder.imagView_goods);
        holder.textView_goods_name.setText(cartInfos.get(position).getGoods_name());
        holder.textView_price.setText(cartInfos.get(position).getGoods_price());
        holder.textView_shuliang.setText(cartInfos.get(position).getGoods_num());
        holder.textView_number.setText(cartInfos.get(position).getGoods_num());
        holder.textView_zhongjia.setText(cartInfos.get(position).getGoods_sum());
        holder.checkBox.setChecked(cartInfos.get(position).isChecked());
    }

    public void OnItemClickListener(ItemClickListener itemClickListener){
        this.itemClickListener = itemClickListener;
    }

    public void OnJiaJianClickListener(JiaJianClickListener jiaJianClickListener){
        this.jiaJianClickListener = jiaJianClickListener;
    }

    @Override
    public int getItemCount() {
        return cartInfos.size();
    }

    public class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView textView_goods_name;
        private CheckBox checkBox;
        private ImageView imagView_goods;
        private TextView textView_price;
        private TextView textView_shuliang;
        private TextView textView_zhongjia;
        private TextView textView_jia;
        private TextView textView_number;
        private TextView textView_jian;
        private ItemClickListener itemClickListener;
        private JiaJianClickListener jiaJianClickListener;

        public CartViewHolder(View itemView,ItemClickListener itemClickListener,
                              JiaJianClickListener jiaJianClickListener) {
            super(itemView);
            textView_goods_name = (TextView) itemView.findViewById(R.id.textView_good_name);
            checkBox = (CheckBox) itemView.findViewById(R.id.checkbox);
            imagView_goods = (ImageView) itemView.findViewById(R.id.imagView_goods);
            textView_price = (TextView) itemView.findViewById(R.id.textView_price);
            textView_shuliang = (TextView) itemView.findViewById(R.id.textView_shuliang);
            textView_zhongjia = (TextView) itemView.findViewById(R.id.textView_zhongjia);
            textView_jia = (TextView) itemView.findViewById(R.id.textView_jia);
            textView_number = (TextView) itemView.findViewById(R.id.textView_number);
            textView_jian = (TextView) itemView.findViewById(R.id.textView_jian);
            checkBox.setOnClickListener(this);
            textView_jia.setOnClickListener(this);
            textView_jian.setOnClickListener(this);
            this.itemClickListener = itemClickListener;
            this.jiaJianClickListener = jiaJianClickListener;
        }

        void bindView(CartInfo cartInfo){
            checkBox.setTag(cartInfo);
            textView_jian.setTag(cartInfo);
            textView_jia.setTag(cartInfo);
        }

        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.checkbox)
                itemClickListener.onItemClick(v, (CartInfo) v.getTag(),getLayoutPosition());
            else
                jiaJianClickListener.onJiaJianClick(v, (CartInfo) v.getTag(),getLayoutPosition());
        }
    }

    public interface ItemClickListener{

        void onItemClick(View view,CartInfo cartInfo,int position);
    }

    public interface JiaJianClickListener{
        void onJiaJianClick(View view,CartInfo cartInfo,int postion);
    }
}
